package com.yh.utils.extract.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.sql.SQLException;

@Component
public class DataSourcePropertes {

    @Autowired
    private Environment env;
    /**
     * 配置数据源基本信息
     * @Title:
     * @Description:
     * @param
     * @return:
     * @throws:
     * @author: maliang
     * @Date: 2019/8/2
     */
    public void setDataSourcePropeties(DruidDataSource dataSource) throws SQLException {
        dataSource.setValidationQuery(this.env.getProperty("spring.datasource.validationQuery"));
        String maxWait = this.env.getProperty("spring.datasource.maxWait");
        if (StringUtils.isNotBlank(maxWait)) {
            dataSource.setMaxWait(Long.parseLong(maxWait));
        }

        String maxActive = this.env.getProperty("spring.datasource.maxActive");
        if (StringUtils.isNotBlank(maxActive)) {
            dataSource.setMaxActive(Integer.parseInt(maxActive));
        }

        String initialSize = this.env.getProperty("spring.datasource.initialSize");
        if (StringUtils.isNotBlank(initialSize)) {
            dataSource.setInitialSize(Integer.parseInt(initialSize));
        }

        String minIdle = this.env.getProperty("spring.datasource.minIdle");
        if (StringUtils.isNotBlank(minIdle)) {
            dataSource.setMinIdle(Integer.parseInt(minIdle));
        }

        String poolPreparedStatements = this.env.getProperty("spring.datasource.poolPreparedStatements");
        if (StringUtils.isNotBlank(poolPreparedStatements)) {
            dataSource.setPoolPreparedStatements(Boolean.parseBoolean(poolPreparedStatements));
        }

        String maxOpenPreparedStatements = this.env.getProperty("spring.datasource.maxOpenPreparedStatements");
        if (StringUtils.isNotBlank(maxOpenPreparedStatements)) {
            dataSource.setMaxOpenPreparedStatements(Integer.parseInt(maxOpenPreparedStatements));
        }

        String timeBetweenEvictionRunsMillis = this.env.getProperty("spring.datasource.timeBetweenEvictionRunsMillis");
        if (StringUtils.isNotBlank(timeBetweenEvictionRunsMillis)) {
            dataSource.setTimeBetweenEvictionRunsMillis(Long.parseLong(timeBetweenEvictionRunsMillis));
        }

        String minEvictableIdleTimeMillis = this.env.getProperty("spring.datasource.minEvictableIdleTimeMillis");
        if (StringUtils.isNotBlank(minEvictableIdleTimeMillis)) {
            dataSource.setMinEvictableIdleTimeMillis(Long.parseLong(minEvictableIdleTimeMillis));
        }

        String testWhileIdle = this.env.getProperty("spring.datasource.testWhileIdle");
        if (StringUtils.isNotBlank(testWhileIdle)) {
            dataSource.setTestWhileIdle(Boolean.parseBoolean(testWhileIdle));
        }

        dataSource.setKeepAlive(true);
        String filters = this.env.getProperty("spring.datasource.filters");
        if (!StringUtils.isNotBlank(filters)) {
            filters = "stat";
        }

        dataSource.setFilters(filters);
        String connectionProperties = this.env.getProperty("spring.datasource.connectionProperties");
        if (!StringUtils.isNotBlank(connectionProperties)) {
            connectionProperties = "druid.stat.mergeSql=true;druid.stat.logSlowSql=true;druid.stat.slowSqlMillis=5000";
        }

        dataSource.setConnectionProperties(connectionProperties);
    }
}
